﻿<template>
    <elm-page-bb id="pass_edit">
        <el-form ref="model" :model="model" size="small" label-width="95px">
            <el-form-item label="当前密码" prop="oldPass" :rules="[{ required: true, message: '当前密码必须填写'}]">
                <el-input v-model="model.oldPass" show-password></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPass" :rules="[{ required: true, message: '新密码必须填写'}]">
                <el-input v-model="model.newPass" show-password></el-input>
            </el-form-item>
            <el-form-item label="重复新密码" prop="newPass_2" :rules="[{ required: true, message: '重复新密码必须填写'}]">
                <el-input v-model="model.newPass_2" show-password></el-input>
            </el-form-item>
        </el-form>
        <div style="margin:10px;color:#666;background:#FAFFBF;padding:10px;line-height:25px;">
            温馨提示：<br />
            1.密码设置要求长度不应小于8位，且须包含英文大写字母、小写字母、数字和特殊字符。<br />
        </div>
        <template #bottom>
            <el-button type="success" size="small" @click="editPass()">确定修改</el-button>
            <el-button size="small" @click="close">取消</el-button>
        </template>
    </elm-page-bb>
</template>

<script>
    export default {
        extends: elmVueBase,
        data() {
            return {
                model: { newPass: null, newPass_2: null, oldPass: null, id: null }
            }
        },
        methods: {
            async editPass() {
                await this.$refs["model"].validate();
                if (this.model.oldPass == this.model.newPass) {
                    this.$message.error('您填写的新旧密码内容相同。');
                    return;
                }
                if (this.model.newPass != this.model.newPass_2) {
                    this.$message.error('两次填写的新密码不一致。');
                    return;
                }
                if(!this.validatePassword(this.model.newPass))
                {
                  this.$message.error("新密码强度不够，请按照提示要求设定新密码。");
                  return;
                }
                //console.log(elm.info.user.id);
                //this.model.id = elm.info.user.id;
                let res = await elm.post('/system/login/EditPassword', this.model)
                if (res.status != 'success') {
                  this.$message.error("新密码强度不够，请按照提示要求设定新密码。");
                  return;
                }
                else {
                    this.$message({ type: 'success', message: '密码修改成功。' });
                    this.close();
                }
            },
          validatePassword(password) {
            const lengthRequirement = password.length >= 8;
            const upperCaseRequirement = /[A-Z]/.test(password);
            const lowerCaseRequirement = /[a-z]/.test(password);
            const numberRequirement = /[0-9]/.test(password);
            const specialCharRequirement = /[!@#$%^&*(),.?":{}|<>]/.test(password);
            return lengthRequirement && upperCaseRequirement && lowerCaseRequirement && numberRequirement && specialCharRequirement;
          }
        }
    }
</script>

<style>
    #pass_edit .el-input__inner, #pass_edit .el-input__inner:hover {
        color: #666 !important;
    }
</style>